package com.coderman.domainservice.blogtopic.app.consumer;

import com.alibaba.fastjson.JSON;
import com.coderman.domainservice.blogtopic.domain.gataway.BlogRepository;
import com.coderman.domainservice.blogtopic.domain.model.entity.BlogAggregateBO;
import com.coderman.domainservice.blogtopic.domain.model.event.consumer.BlogCheckResultEvent;
import com.coderman.domainservice.blogtopic.domain.model.event.provider.PublishBlogEvent;
import com.coderman.domainservice.blogtopic.domain.model.valueobject.BlogStatusEnum;
import com.coderman.domainservice.blogtopic.infrast.mq.Constants;
import com.coderman.domainservice.blogtopic.infrast.mq.provider.MqSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Description:
 * date: 2021/3/30
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 * 博客内容审核结果消息消费处理逻辑
 */
@Service
public class BlogCheckResultService {

    @Autowired
    private BlogRepository blogRepository;
    @Autowired
    private MqSendService mqSendService;
    /**
     * 处理审核结果消息
     * @param blogCheckResultEvent
     */
    public void dealMessage(BlogCheckResultEvent blogCheckResultEvent){
        //审核通过
        //1.更新博客状态
        blogRepository.dealBlog(blogCheckResultEvent.getBlogId(), BlogStatusEnum.PUBLISHED.getStatus());

        if(blogCheckResultEvent.getCode().equals("SUCCESS")){
            PublishBlogEvent publishBlogEvent = new PublishBlogEvent();
            publishBlogEvent.setBlogId(blogCheckResultEvent.getBlogId());
            BlogAggregateBO blogAggregateBO = blogRepository.findOneBlog(blogCheckResultEvent.getBlogId());
            publishBlogEvent.setUserId(blogAggregateBO.getBlogBO().getUserId());
            publishBlogEvent.setPublishTime(new Date());
            publishBlogEvent.setPublishType(0);
            //3.发送博客发布成功消息
            mqSendService.send(Constants.TOPIC_CENTER, JSON.toJSONString(publishBlogEvent));
        }else {
            //2.给用户发送审核失败提醒
        }
    }
}
